#pragma once

#include <memory>
#include <string>
#include <sstream>
#include <thread>
#include <spdlog/spdlog.h>
#include <sys/types.h>
#include "namespace_manager.h"
#include "cgroup_manager.h"

namespace judge {

class SandboxManager {
public:
    SandboxManager();
    ~SandboxManager() = default;

    // 设置命令
    void setCommand(const std::string& cmd) { command_ = cmd; }
    
    // 设置工作目录
    void setWorkingDirectory(const std::string& dir) { working_dir_ = dir; }
    
    // 设置资源限制
    void setCPUTimeLimit(int seconds) { cpu_time_limit_ = seconds; }
    void setMemoryLimit(int mb) { memory_limit_ = mb; }
    void setOutputLimit(int kb) { output_limit_ = kb; }
    
    // 执行命令
    int run();
    
    // 获取输出
    const std::string& getStdout() const { return stdout_; }
    const std::string& getStderr() const { return stderr_; }

private:
    int executeCommand(pid_t pid);
    void setResourceLimits();

    std::unique_ptr<NamespaceManager> namespace_manager_;
    std::unique_ptr<CgroupManager> cgroup_manager_;
    
    std::string command_;
    std::string working_dir_;
    std::string stdout_;
    std::string stderr_;
    
    int cpu_time_limit_{0};
    int memory_limit_{0};
    int output_limit_{0};
};

} // namespace judge 